package com.web.adm.controller;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.web.annotation.domain.UserBody;
import com.web.annotation.domain.UserDomain;
import com.web.annotation.templete.BodyMethod;
import com.web.annotation.templete.User;
import com.web.annotation.untils.AnnotationUtils;
import com.web.security.user.WebUser;
import com.web.security.util.SecurityUtils;

@Controller
public class AdminController {

	@RequestMapping(value="/" , method=RequestMethod.GET)
	public ModelAndView init(){
		
		return new ModelAndView("redirect:login.html");
	}
	
	@RequestMapping(value="/index.html" , method=RequestMethod.GET)
	public ModelAndView index(){
		
		return new ModelAndView("login");
	}
	
	@RequestMapping(value="/login.html" , method=RequestMethod.GET)
	public ModelAndView logins(){

		WebUser webUser = SecurityUtils.getUser();
		if(webUser != null){
			return new ModelAndView("redirect:admin/home.html");
		}else{
			return new ModelAndView("login");
		}
	}
	
	@RequestMapping(value="/admin/home.html" , method=RequestMethod.GET)
	public ModelAndView home(HttpServletRequest request,HttpServletResponse response){
		ModelAndView model = new ModelAndView();
		User user = (User)AnnotationUtils.getAnnotation(UserDomain.class, User.class);
		
		model.getModel().put("name", user.name());
		model.getModel().put("age", user.age());
		
		List<Method> methods = new ArrayList();
		methods.addAll(AnnotationUtils.getMethods(UserBody.class));
		
		for(Method method : methods){
			if(method.isAnnotationPresent(BodyMethod.class)){
				System.out.println("Name : "+method.getName());
				System.out.println("DeclaringClass : "+method.getName());
				System.out.println("DeclaringClass : "+method.getAnnotation(BodyMethod.class).methodName());
				System.out.println("DeclaringClass : "+method.getAnnotation(BodyMethod.class).methodType());
				System.out.println();
				System.out.println();
			}
		}
	
		model.getModel().put("message", "Home Page");
		
		return model;
	}
	
	
	@RequestMapping(value="/accessDenied.html",method=RequestMethod.GET )
	public ModelAndView accessDenied(){
		ModelAndView model = new ModelAndView("home");
		model.getModel().put("massage", "Access Denied");
		return model;
	}
}
